package com.yinhe.module.dr;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.exception.ServiceException;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhVoteOption;
import com.yinhe.system.domain.YhVotePeople;
import com.yinhe.system.service.IYhVoteOptionService;
import com.yinhe.system.service.IYhVotePeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 投票选项Controller
 *
 * @author yinhe
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/dr/option")
public class DrYhVoteOptionController extends BaseController {
    @Autowired
    private IYhVoteOptionService yhVoteOptionService;
    @Autowired
    private IYhVotePeopleService yhVotePeopleService;

    /**
     * 查询投票选项列表
     */
    @PreAuthorize("@ss.hasPermi('dr:option:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhVoteOption yhVoteOption) {
        startPage();
        List<YhVoteOption> list = yhVoteOptionService.selectYhVoteOptionList(yhVoteOption);
        return getDataTable(list);
    }

    /**
     * 导出投票选项列表
     */
    @PreAuthorize("@ss.hasPermi('dr:option:export')")
    @Log(title = "投票选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhVoteOption yhVoteOption) {
        List<YhVoteOption> list = yhVoteOptionService.selectYhVoteOptionList(yhVoteOption);
        ExcelUtil<YhVoteOption> util = new ExcelUtil<YhVoteOption>(YhVoteOption.class);
        util.exportExcel(response, list, "投票选项数据");
    }

    /**
     * 获取投票选项详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(yhVoteOptionService.selectYhVoteOptionById(id));
    }

    /**
     * 新增投票选项
     */
    @PreAuthorize("@ss.hasPermi('dr:option:add')")
    @Log(title = "投票选项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhVoteOption yhVoteOption) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhVoteOption.setUserId(sysUser.getUserId());
        yhVoteOption.setDeptId(sysUser.getDeptId());
        yhVoteOption.setOptionNum("0");
        return toAjax(yhVoteOptionService.insertYhVoteOption(yhVoteOption));
    }

    /**
     * 修改投票选项
     */
    @PreAuthorize("@ss.hasPermi('dr:option:edit')")
    @Log(title = "投票选项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhVoteOption yhVoteOption) {

        return toAjax(yhVoteOptionService.updateYhVoteOption(yhVoteOption));
    }

    /**
     * 投票
     */
    @PreAuthorize("@ss.hasPermi('dr:option:edit:tp')")
    @Log(title = "投票选项", businessType = BusinessType.UPDATE)
    @PutMapping("/tp")
    public AjaxResult tp(@RequestBody YhVoteOption yhVoteOption) {
        int vt = Integer.parseInt(yhVoteOption.getVoteType());
        System.out.println(vt +"vtvtvtvvtvtvtvvtvtvt");
        if (vt==1){
            Radio(yhVoteOption);
        } else if (vt==2) {
            MultiSelect(yhVoteOption);

        }

        return toAjax(yhVoteOptionService.updateYhVoteOption(yhVoteOption));


    }

        private void MultiSelect(YhVoteOption yhVoteOption){
            // 获取当前登录用户
            LoginUser loginUser = getLoginUser();
            SysUser sysUser = loginUser.getUser();
            YhVotePeople yhVotePeople = new YhVotePeople();
            // 查询YhVotePeople人表中的userId
            List<YhVotePeople> list = yhVotePeopleService.selectYhVotePeopleByUserId();
            List<String> list_jh = new ArrayList<>();

            for (int i =0; i<list.size();i++){
                String str= String.valueOf(list.get(i).getUserId());
                String str1= String.valueOf(list.get(i).getVoteOptionId());
                String str2 = str +str1;
                list_jh.add(str2);
            }

            String str3 = String.valueOf(sysUser.getUserId());
            String str4 = String.valueOf(yhVoteOption.getId());
            String str5 = str3 + str4;

            //判断是否投票 &&list_voteOptionId.contains(yhVoteOption.getVoteId())
            if (list_jh.contains(str5)) {
                throw new ServiceException("您已经投过票了！！！");
            } else {
                yhVotePeople.setUserId(sysUser.getUserId());
                yhVotePeople.setDeptId(sysUser.getDeptId());
                yhVotePeople.setNickName(sysUser.getNickName());
                yhVotePeople.setVoteId(yhVoteOption.getVoteId());
                yhVotePeople.setVoteType(yhVoteOption.getVoteType());
                yhVotePeople.setVoteOptionId(yhVoteOption.getId());
                yhVotePeople.setVoteOption(yhVoteOption.getVoteOption());
                //yhVotePeople.setNote1("1");
                String str = yhVoteOption.getOptionNum();
                System.out.println(str);
                int t = Integer.parseInt(str);
                t = ++t;
                String s = String.valueOf(t);
                yhVoteOption.setOptionNum(s);
                //yhVoteOption.setNote("已投票");
                yhVotePeopleService.insertYhVotePeople(yhVotePeople);

            }
        }
    private void Radio(YhVoteOption yhVoteOption){
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        YhVotePeople yhVotePeople = new YhVotePeople();
        // 查询YhVotePeople人表中的userId
        List<YhVotePeople> list = yhVotePeopleService.selectYhVotePeopleByUserId();
        List<String> list_jh = new ArrayList<>();

        for (int i =0; i<list.size();i++){
            String str= String.valueOf(list.get(i).getUserId());
            String str1= String.valueOf(list.get(i).getVoteId());
            System.out.println("str1str1str1str1str1"+str1);
            String str2 = str +str1;
            list_jh.add(str2);
        }

        String str3 = String.valueOf(sysUser.getUserId());
        String str4 = String.valueOf(yhVoteOption.getVoteId());
        System.out.println("str4str4str4"+str4);
        String str5 = str3 + str4;

        //判断是否投票 &&list_voteOptionId.contains(yhVoteOption.getVoteId())
        if (list_jh.contains(str5)) {
            throw new ServiceException("该投票为单选您只能投一次票！！！");
        } else {
            yhVotePeople.setUserId(sysUser.getUserId());
            yhVotePeople.setDeptId(sysUser.getDeptId());
            yhVotePeople.setNickName(sysUser.getNickName());
            yhVotePeople.setVoteId(yhVoteOption.getVoteId());
            yhVotePeople.setVoteType(yhVoteOption.getVoteType());
            yhVotePeople.setVoteOptionId(yhVoteOption.getId());
            yhVotePeople.setVoteOption(yhVoteOption.getVoteOption());
            //yhVotePeople.setNote1("1");
            String str = yhVoteOption.getOptionNum();
            System.out.println(str);
            int t = Integer.parseInt(str);
            t = ++t;
            String s = String.valueOf(t);
            yhVoteOption.setOptionNum(s);
           // yhVoteOption.setNote("已投票");
            yhVotePeopleService.insertYhVotePeople(yhVotePeople);

        }
    }
    /**
     * 删除投票选项
     */
    @PreAuthorize("@ss.hasPermi('dr:option:remove')")
    @Log(title = "投票选项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhVoteOptionService.deleteYhVoteOptionByIds(ids));
    }


}


